<?php
require_once('include/inc_database.php');
require_once('include/inc_recommendation.php');
require_once('include/inc_reccomment.php');


//echo "temporarily disabled.";
//exit();


// Open DB connection.
$dbobj = new DatabaseObject();
$dbobj->OpenConnection();

header('Content-Type: application/xml');

//$user_ar = $dbobj->GetResultsAsNestedArray("SELECT links.* FROM links WHERE username = $username;", 1);
//$user_r = $user_ar[0];

// Note: not using GetRecArray() because it doesn't return
// disabled recommendations, which we need.
$sql = "SELECT rec.*, usr.userIconPath, " .
		"(SELECT COUNT(*) FROM mr_recComment cmt WHERE cmt.recID = rec.recID AND cmt.disabled = 0) AS comment_count ";
if ($_GET['all'] != "") {
	$sql .= "FROM mr_recommendation rec " .
		"INNER JOIN mr_useraccount usr ON rec.username = usr.username " .
		"ORDER BY recID DESC";
	$feed_title = SiteConst('site_name') . " - all";
}
elseif ($_GET['user'] != "") {
	$username = $_GET['user'];
	$sql .= "FROM mr_recommendation rec " .
		"INNER JOIN mr_useraccount usr ON rec.username = usr.username " .
		"WHERE username = " . ValToSQL_str($username) . " " .
		"ORDER BY recID DESC";
	$feed_title = SiteConst('site_name') . " - " . $username . "";
}
elseif ($_GET['user_in'] != "") {
	$username = $_GET['user_in'];
	$sql .= "FROM mr_recommendation rec " .
		"INNER JOIN mr_useraccount usr ON rec.username = usr.username " .
		"INNER JOIN mr_subscription sub ON rec.username = sub.subscribedToUsername " .
		"WHERE sub.username = " . ValToSQL_str($username) . " " .
		"ORDER BY recID DESC";
	$feed_title = SiteConst('site_name') . " - incoming";
}
$rec_ar = $dbobj->GetResultsAsNestedArray($sql, 50);




// Start output.
echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
?>

<rss version='2.0'>
<channel>
<title><?=htmlspecialchars($feed_title)?></title>
<description></description>
<link>http://plurib.us/musicrecommend/user.php?user=<?=$username?></link>



<?$i = 0;
foreach ($rec_ar as $rec_r) {
	$i++;
	if ($rec_r['disabled']) {?>
		<item>
			<title>Deleted recommendation</title>
			<!--<link>http://plurib.us/musicrecommend/rec.php?recID=<?=$rec_r['recID']?></link>-->
			<guid><?=$rec_r['recID']?></guid>
			<!--<author><?=htmlspecialchars($rec_r['username'])?></author>-->
			<pubDate><?=date("Y-m-d", strtotime($rec_r["addedDateTime"]))?></pubDate>
		</item>
	<?}
	else {?>
		<item>
			<title><?=htmlspecialchars($rec_r['recText'])?></title>
			<link>http://plurib.us/musicrecommend/rec.php?recID=<?=$rec_r['recID']?></link>
			<guid><?=$rec_r['recID']?></guid>
			<!--<author><?=htmlspecialchars($rec_r['username'])?></author>-->
			<pubDate><?=date("Y-m-d", strtotime($rec_r["addedDateTime"]))?></pubDate>
			<description>
				<?=htmlspecialchars(Recommendation::DescriptionHtml($rec_r['description']))?>
				<?echo htmlspecialchars("<div><i>");
				echo htmlspecialchars("by ");
				if ($rec_r['userIconPath'] != "") {
					echo  htmlspecialchars("<a href='user.php?user=" . $rec_r['username'] . "'><img src='image.php?usericon=" . $rec_r['userIconPath'] . "&amp;size=16' align='ABSMIDDLE' style='border:solid 1px #999;'/></a> ");
				}
				echo htmlspecialchars("<a href='user.php?user=" . $rec_r['username'] . "'>" . $rec_r['username'] . "</a>");
				if ($rec_r['comment_count'] > 0) {
					echo htmlspecialchars("&nbsp; &nbsp;<a href='http://plurib.us/musicrecommend/rec.php?recID=" . $rec_r['recID'] . "'>" . $rec_r['comment_count'] . " comments</a>");
				}
				echo htmlspecialchars("</i></div>");?>
			</description>
		</item>
	<?}?>
<?}?>

</channel>
</rss>
